package com.oscar.web.service;


import com.oscar.web.dao.UserRepository;
import com.oscar.web.domain.MyUserDetails;
import com.oscar.web.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class MyUserDetailsService implements UserDetailsService {
    //private String username ;

    @Autowired
    private UserRepository userRepository ;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //this.username = username;
        Users user = userRepository.findByUsername(username, Users.class) ;
        if(user == null){
            throw new UsernameNotFoundException(username + "不存在") ;
        }
        //将username，user.getPassword()传入userDetails，将userDetails的信息与用户输入的账号密码进行匹配
        UserDetails userDetails = new MyUserDetails(username, user.getPassword()) ;
        return userDetails;
    }
}
